package Demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.NoSuchElementException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.Test;


@Test
public class Dice extends DemoClass {

	/**
	 * @param args
	 */
   public WebDriver myWD = new HtmlUnitDriver();
   public int xlRowTS, xlColTS, xlRowTC, xlColTC, xlRowTD, xlColTD, xlRow, xlCol;
   public String[][] xlTCData, xlTSData, xlTDData;
   public String vKeyword, vIP1, vIP2, vIP3;
   public String vTDID, vExecute, vURL, vBrowser, vName, vUserName, vPswd, vEmail, vCity, vzip, vTechnology, vSearch;
   public long vZip;
   public int i,j;
   public String vTSResult, vTSV_res ,vTC_res;
	/**
	 * @param args
	 * @throws Exception 
	 */
	public void testDice() throws Exception
	{
		// TODO Auto-generated method stub
		xlReadTD("C:/Manjusha/Selenium/SeleniumQtpelearn/Automation Plan.xls");
		for(int xlTD=1;xlTD<xlRowTD;xlTD++)
		{
			if(xlTDData[xlTD][1].equals("Y"))
			{
				vURL= xlTDData[xlTD][2];
				vTechnology= xlTDData[xlTD][10];
				vCity= xlTDData[xlTD][8];
				vzip=xlTDData[xlTD][9];
			    //vZip = Long.valueOf(vzip);

			    
			    
			    Double vzzip= Double.valueOf(vzip);
			       vZip = (new Double(vzzip)).longValue();
				xlReadTC("C:/Manjusha/Selenium/SeleniumQtpelearn/Automation Plan.xls");
	    	
				for(int xlTC=1;xlTC< xlRowTC; xlTC++ )
	    	{
	    		if(xlTCData[xlTC][4].equals("Y"))
	    		{
	    			xlReadTS("C:/Manjusha/Selenium/SeleniumQtpelearn/Automation Plan.xls");
	    		for(int xlTS=1;xlTS<xlRowTS;xlTS++)
	    		{
	    			if(xlTSData[xlTS][0].equals(xlTCData[xlTC][1]))
	    				{
	    					vKeyword= xlTSData[xlTS][4];
	    					vIP1= xlTSData[xlTS][5];
	    					vIP2=xlTSData[xlTS][6];
	    					vIP3=xlTSData[xlTS][7];
	    					vTSResult = "Pass";
	    					vTSV_res = "Pass";
							vTC_res="Pass";
							getIP();
	    					try
	    					{
	    					keyword_executor();
	    					}	
	    					catch (NoSuchElementException e1) 
	    					{
	    						System.out.println("App Error occurred " + e1);	
	    						vTSResult="Fail";
	    						xlTSData[xlTS][9]="Error is:"+e1;
	    			    	}
	    					if(vTSResult.equals("Fails")|| vTSV_res.equals("Fail"))
	    					{
							vTC_res = "Fail";
							xlTSData[xlTS][8]= "Fail";
						    }	
		    				else
		    				{
		    					xlTSData[xlTS][8]= "Pass";
		    				}
	    			  }
	    		}
	    	xlTCData[xlTC][5]= vTC_res;
	    
	    	}
	    	xlWrite_tc("C:/Manjusha/Selenium/SeleniumQtpelearn/Automation Plan TC Result.xls", xlTCData);
	    	xlWrite_ts("C:/Manjusha/Selenium/SeleniumQtpelearn/Automation Plan TS Result.xls", xlTSData);
	    		
	      }
		}
	}
}

public void navigate_to(WebDriver fWD, String fURL )
{
	fWD.navigate().to(fURL);
}

public void send_keys(WebDriver fWD, String fCSS, String fText){
	fWD.findElement(By.cssSelector(fCSS)).sendKeys(fText);
	
}

public void button_click(WebDriver fWD, String fCSS)
{
	fWD.findElement(By.cssSelector(fCSS)).click();
}
public String link_check(WebDriver fWD, String fText){
	try
	{
	if(fWD.findElement(By.linkText(fText)).isDisplayed())
	{
		return "Pass";
	}
	else
	{
		return "Fail";
	}
	}
	catch (NoSuchElementException e) {
		System.out.println("Element not found");
		System.out.println("Error is " + e);	
		return "Fail";
	}
}
public void link_click(WebDriver fWD, String fCSS){
	fWD.findElement(By.cssSelector(fCSS)).click();
}
public void click_element(WebDriver fWD, String fCSS){
	try 
	{
		
			fWD.findElement(By.cssSelector(fCSS)).click();
	}
	catch (NoSuchElementException e) {
			System.out.println("Element not found");
			System.out.println("Error is " + e);	
			
		}
	
}

public String verify_text(WebDriver fWD, String fCSS){
	try 
	{
		if(fWD.findElement(By.cssSelector(fCSS)).isDisplayed()){
			return "Pass";
		}
		else{
			return "Fail";
		}
		
	}
	catch (NoSuchElementException e) {
		System.out.println("Element not found");
		System.out.println("Error is " + e);	
		return "Fail";
	}
}

public String get_text(WebDriver fWD, String fCSS){
	try{
	String ftext =fWD.findElement(By.cssSelector(fCSS)).getText();
	return ftext;
	}
	catch (NoSuchElementException e) {
		System.out.println("Text element not found");
		System.out.println("Error is " + e);
		return "Fail";
	}
}
	
public String verify_attribute(WebDriver fWD, String fCSS, String fAttribute, String fText){
	try 
	{
	String fAttri=fWD.findElement(By.cssSelector(fCSS)).getAttribute(fAttribute);
		
		if(fAttri.equals(fText)){
			return "Pass";
		}
		else{
			return "Fail";
		}
		
	}
	catch (NoSuchElementException e) {
		System.out.println("Element not found");
		System.out.println("Error is " + e);	
		return "Fail";
	}
}
public void xlReadTD(String fxPath) throws Exception{
	File input = new File(fxPath);
	FileInputStream myStream = new FileInputStream(input);
	HSSFWorkbook myWB = new HSSFWorkbook(myStream);
	HSSFSheet mySheet = myWB.getSheet("TestData");
	
	xlRowTD = mySheet.getLastRowNum()+1;
	xlColTD = mySheet.getRow(0).getLastCellNum();
	xlTDData = new String [xlRowTD][xlColTD];
	for(int i=0; i<xlRowTD; i++)
	{
		HSSFRow row = mySheet.getRow(i);
		for(int j=0;j<xlColTD;j++){
			HSSFCell col = row.getCell(j);
			String value = cell_to_String(col);
			xlTDData[i][j] = value;
		}
	}
}
public void xlReadTC(String fxPath) throws Exception{
	File input = new File(fxPath);
	FileInputStream myStream = new FileInputStream(input);
	HSSFWorkbook myWB = new HSSFWorkbook(myStream);
	HSSFSheet mySheet = myWB.getSheet("TestCases");
	
	xlRowTC = mySheet.getLastRowNum()+1;
	xlColTC = mySheet.getRow(0).getLastCellNum();
	xlTCData = new String [xlRowTC][xlColTC];
	for(int i=0; i<xlRowTC; i++)
	{
		HSSFRow row = mySheet.getRow(i);
		for(int j=0;j<xlColTC;j++){
			HSSFCell col = row.getCell(j);
			String value = cell_to_String(col);
			xlTCData[i][j] = value;
		}
	}
}

public String cell_to_String(HSSFCell col){
	Object result;
	int type = col.getCellType();
	switch (type){
	case HSSFCell.CELL_TYPE_STRING:
		result = col.getStringCellValue();
		break;
	case HSSFCell.CELL_TYPE_NUMERIC:
		result = col.getNumericCellValue();
		break;
	case HSSFCell.CELL_TYPE_BLANK:
		result = "-";
		break;
	case HSSFCell.CELL_TYPE_BOOLEAN:
		result = col.getBooleanCellValue();
		break;
	case HSSFCell.CELL_TYPE_ERROR:
		throw new RuntimeException ("This cell has an error");
	case HSSFCell.CELL_TYPE_FORMULA:
		throw new RuntimeException("We can't evaluate formulas in Java");
	default:
		throw new RuntimeException("We don't support this cell type: " + type);
		
	}
	return result.toString();
}
public void xlReadTS(String fxPath) throws Exception{
	File input = new File(fxPath);
	FileInputStream inStream = new FileInputStream(input);
	HSSFWorkbook myWB = new HSSFWorkbook(inStream);
	HSSFSheet mySheet = myWB.getSheet("TestSteps");
	xlRowTS = mySheet.getLastRowNum()+1;
	xlColTS = mySheet.getRow(0).getLastCellNum();
	xlTSData= new String[xlRowTS][xlColTS];
	for (i=0;i<xlRowTS;i++){
		HSSFRow myRowTS = mySheet.getRow(i);
		for(j=0;j<xlColTS;j++){
			HSSFCell myCellTS = myRowTS.getCell(j) ;
			String value = cell_to_String(myCellTS);
			xlTSData[i][j]=value;
		}
	}
}
public void xlWrite_tc(String fxPath, String[][] fxlDataTC) throws Exception{
	File output = new File(fxPath);
	//FileInputStream instream = new FileInputStream(output);
	HSSFWorkbook myWB = new HSSFWorkbook();
	HSSFSheet mySheet= myWB.createSheet("Test_Result");
	for(i=0;i<xlRowTC;i++)
	{
	HSSFRow xlRow = mySheet.createRow(i);
		for(j=0;j<xlColTC;j++)
		{
			HSSFCell col = xlRow.createCell(j);
			col.setCellType(HSSFCell.CELL_TYPE_STRING);
			col.setCellValue(fxlDataTC[i][j]);
		}
	}
	FileOutputStream myOutStream = new FileOutputStream(output);
	myWB.write(myOutStream);
	myOutStream.flush();
	myOutStream.close();
}
public void xlWrite_ts(String fxPath, String[][] fxlDataTS) throws Exception{
	File output = new File(fxPath);
	HSSFWorkbook myWB = new HSSFWorkbook();
	HSSFSheet mySheet= myWB.createSheet("Test_Result");
	for(i=0;i<xlRowTS;i++)
	{
	HSSFRow xlRow = mySheet.createRow(i);
		for(j=0;j<xlColTS;j++)
		{
			HSSFCell col = xlRow.createCell(j);
			col.setCellType(HSSFCell.CELL_TYPE_STRING);
			col.setCellValue(fxlDataTS[i][j]);
		}
	}
	FileOutputStream myOutStream = new FileOutputStream(output);
	myWB.write(myOutStream);
	myOutStream.flush();
	myOutStream.close();
}
public void close_browser(WebDriver fWD){
	fWD.close();
}

public void getIP(){
	// Identify the TD Variables mentioned in the TS Excel
	// And accordingly assign the Test Data into IP1,2,3.
	if (vIP1.equals("vURL")){
		vIP1 = vURL;
	}
	if (vIP1.equals("vTechnology")){
		vIP1 = vTechnology;
	}
	if (vIP1.equals("vSearch")){
		vIP1 = vSearch;
	}
	if (vIP1.equals("vCity")){
		vIP1 = vCity;
	}
	if (vIP1.equals("vZip")){
		vIP1 = String.valueOf(vZip);
	}
	if (vIP2.equals("vURL")){
		vIP2 = vURL;
	}
	if (vIP2.equals("vTechnology")){
		vIP2 = vTechnology;
	}
	if (vIP2.equals("vSearch")){
		vIP2 = vSearch;
	}
	if (vIP2.equals("vCity")){
		vIP2 = vCity;
	}
	if (vIP2.equals("vZip")){
		vIP2 = String.valueOf(vZip);
	}
	if (vIP3.equals("vURL")){
		vIP3 = vURL;
	}
	if (vIP3.equals("vTechnology")){
		vIP3 = vTechnology;
	}
	if (vIP3.equals("vSearch")){
		vIP3 = vSearch;
	}
	if (vIP3.equals("vCity")){
		vIP3 = vCity;
	}
	if (vIP3.equals("vZip")){
		vIP3 = String.valueOf(vZip);
	}
}
public void keyword_executor() throws Exception{
	if(vKeyword.equals("navigate_to")){
		//beforelaunchBrowser();
		myWD = new FirefoxDriver();
		navigate_to(myWD, vIP1);
		Thread.sleep(5000);
	}
	if(vKeyword.equals("send_keys")){
		send_keys(myWD, vIP1, vIP2);

	}
	
	if(vKeyword.equals("click_element")){
		click_element(myWD, vIP1);

	}
	if (vKeyword.equals("verify_attribute")){
		verify_attribute(myWD, vIP1, vIP2,vIP3);

	}
	if (vKeyword.equals("close_browser")){
		close_browser(myWD);
	}
	
}
}
